/*
 * Copyright (c) 2010-2020 DMGIS Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * DMGIS. You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the agreements
 * you entered into with DMGIS.
 *
 */
package com.dmgis.qxfw.dataservice.businessservice.yysl.service;

import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.RequestDataBySelect;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.county;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.tour_stations;
import com.dmgis.qxfw.dataservice.businessservice.common.util.JsonUtil;
import com.dmgis.qxfw.dataservice.businessservice.common.util.StringUtil;
import com.dmgis.qxfw.dataservice.businessservice.controller.ProcessRequestController;
import com.fasterxml.jackson.core.type.TypeReference;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommonDao {
    String result = "";

    static {
        SetStation();
        SetTourStation();
    }

    /**
     * 实况站点哈希表
     *
     * @author dsq
     * @date 2018年10月31日
     */
    public static HashMap<String, String> stationList;

    /**
     * 旅游站点哈希表
     *
     * @author dsq
     * @date 2018年10月31日
     */
    public static HashMap<String, String> tourStaList;

    /**
     * 初始化实况站点
     *
     * @author dsq
     * @date 2018年10月31日
     */
    private static void SetStation() {
        ProcessRequestController procon = new ProcessRequestController();
        String result = "";
        stationList = new HashMap<String, String>();
        try {
            RequestDataBySelect dataBySelect = new RequestDataBySelect();
            dataBySelect.setColumns(new String[]{"StationID,StationName"});
            Map<String, String> map = new HashMap<String, String>();
            map.put("param", JsonUtil.object2Json(dataBySelect));
            map.put("method", "smallscale_station");
            map.put("sqlType", "select");
            result = procon.processRequest(map);
            if (result.equals("")){
                result="[]";
            }
            List<tour_stations> list = new ArrayList<tour_stations>();
            list =JsonUtil.jsonStr2TypeReference(result, new TypeReference<List<tour_stations>>(){});
            for (tour_stations t:list) {
                stationList.put(t.getStationname().trim(), t.getStationid().trim());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化旅游站点
     *
     * @author dsq
     * @date 2018年10月31日
     */
    private static void SetTourStation() {
        String result = "";
        ProcessRequestController prco = new ProcessRequestController();
        tourStaList = new HashMap<String, String>();
        try {
            RequestDataBySelect dataBySelect = new RequestDataBySelect();
            dataBySelect.setColumns(new String[]{"StationID,StationName"});
            Map<String, String> map = new HashMap<String, String>();
            map.put("param", JsonUtil.object2Json(dataBySelect));
            map.put("method", "tour_stations");
            map.put("advCode", DbConfig.advCode);
            map.put("sqlType", "select");
            result = prco.processRequest(map);
            if (result.equals("")) {
                result = "[]";
            }
            List<tour_stations> list = new ArrayList<tour_stations>();
            list = JsonUtil.jsonStr2TypeReference(result, new TypeReference<List<tour_stations>>() {
            });
            for (tour_stations t : list) {
                tourStaList.put(t.getStationname().trim(), t.getStationid().trim());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getGoverInfo(String county) {
        ProcessRequestController prco = new ProcessRequestController();
        String result = "";
        try {
            RequestDataBySelect dataBySelect1 = new RequestDataBySelect();
            dataBySelect1.setColumns(new String[]{"distinct Province"});
            dataBySelect1.setParam("city like '%" + county + "%'");
            Map<String, String> map1 = new HashMap<String, String>();
            map1.put("param", JsonUtil.object2Json(dataBySelect1));
            map1.put("method", "County");
            map1.put("sqlType", "select");
            String result1 = "";
            result1 = prco.processRequest(map1);
            if (result1.equals("")) {
                result1 = "[]";
            }
            List<county> list = new ArrayList<county>();
            list = JsonUtil.jsonStr2TypeReference(result1, new TypeReference<List<county>>() {
            });

            String Province = "";
            if (list.size() > 0) {
                Province = list.get(0).getProvince();
            }
            if (StringUtil.IsNullOrEmpty(Province)) {
                RequestDataBySelect dataBySelect = new RequestDataBySelect();
                dataBySelect.setColumns(new String[]{"Province,City"});
                dataBySelect.setParam("NAME like '%" + county + "%'");
                Map<String, String> map = new HashMap<String, String>();
                map.put("param", JsonUtil.object2Json(dataBySelect));
                map.put("method", "County");
                map.put("sqlType", "select");
                String result2 = "";
                result2 = prco.processRequest(map);
                if (result2.equals("")) {
                    result2 = "[]";
                }
                List<county> list1 = new ArrayList<county>();
                list1 = JsonUtil.jsonStr2TypeReference(result2, new TypeReference<List<county>>() {
                });
                result += list1.get(0).getCity().trim() + "|" + list1.get(0).getProvince().trim();
            } else {
                result += county + "|" + Province;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
}
